import Cookies from "js-cookie";
export default {
    state () {
        return {
            isCollapse: false, // 控制菜单展开或收缩
            tabList: [
                {
                    path: '/',
                    name: 'home',
                    label: '首页',
                    icon: 's-home',
                    url: 'Home/Home'
                }
            ], // 面包屑
            menu: [],
        }
    },
    mutations: {
        // 修改isCollapse方法
        changeIsCollapse(state) {
            state.isCollapse = !state.isCollapse;
        },
        selectMenu(state, val) {
            // console.log(val, 'val');
            if(val.name !== "home") {
                const index = state.tabList.findIndex(item => item.name === val.name) // 找不到返回-1
                // 如果不存在
                if(index === -1) {
                    state.tabList.push(val);
                }
            }
        },
        closeTag(state, index) {
            state.tabList.splice(index, 1);
        },
        setMenu(state, val) {
            state.menu = val;
            Cookies.set('menu', JSON.stringify(val));
        },
        addMenu(state, router) {
            // 判断缓存中是否有数据
            if(!Cookies.get('menu')) return;
            const menu = JSON.parse(Cookies.get("menu"));
            state.menu = menu;
            // 组装动态路由的数据
            const menuArray = [];
            menu.forEach(item => {
                if(item.children) {
                    item.children = item.children.map(item => {
                        item.component = () => import(`@/view/${item.url}`);
                        return item
                    })
                    menuArray.push(...item.children);
                } else {
                    item.component = () => import(`@/view/${item.url}`);
                    menuArray.push(item);
                }
            })
            console.log('menuArray', menuArray);
            // 路由的动态添加
            menuArray.forEach(item => {
                router.addRoute('Main', item);
            })
        }
    }
}